/**
 * 
 */
package com.hanhai.zrb.api.facade.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanhai.zrb.api.biz.service.CommonCounterService;
import com.hanhai.zrb.api.biz.service.FundService;
import com.hanhai.zrb.api.biz.service.IntentionService;
import com.hanhai.zrb.api.biz.service.ProjectService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.api.client.facade.UserStatisticDetailFacade;
import com.hanhai.zrb.api.client.result.CollectionResult;
import com.hanhai.zrb.model.common.CommonCounterKey;
import com.hanhai.zrb.model.common.enums.StatusEnums;
import com.hanhai.zrb.model.fund.Fund;
import com.hanhai.zrb.model.fund.dto.FundDto;
import com.hanhai.zrb.model.intention.Intention;
import com.hanhai.zrb.model.project.Project;
import com.hanhai.zrb.model.user.UserStatisticDetail;
import com.hanhai.zrb.model.usercenter.CommonStatistic;
import com.hanhai.zrb.model.usercenter.IntentionStatistic;
import com.hanhai.zrb.model.usercenter.MyList;

/**
 * @author lixiong
 *
 */
@WebService(endpointInterface = "com.hanhai.zrb.api.client.facade.UserStatisticDetailFacade", serviceName = "UserStatisticDetailFacade")
@Service("userStatisticDetailFacade")
public class UserStatisticDetailFacadeImpl implements UserStatisticDetailFacade {

	private Logger logger = Logger.getLogger(getClass());
	
	@Resource
	private UserStatisticDetailService userStatisticDetailService;
	
	@Autowired
	private ProjectService projectService;
	
	@Resource
	private IntentionService intentionService;
	
	@Resource
	private FundService fundService;
	
	@Resource
	private CommonCounterService commonCounterService;
	
	
	
	@Override
	public List<CommonStatistic> getUserCollectionProjects(Long userId,
			Date maxCreateTime,Integer limit) {
		List<CommonStatistic> list = new ArrayList<CommonStatistic>();
		try {
			list = userStatisticDetailService.getUserCollectionProject(
					userId, maxCreateTime, limit);
		} catch (Exception e) {
			logger.error("userCollectionService.getUserCollectionProject", e);
		}
		return list;
		
	}
	
	@Override
	public List<CommonStatistic> getUserCollectionFund(Long userId,Date maxCreateTime,Integer limit){
		List<CommonStatistic> list = new ArrayList<CommonStatistic>();
		try {
			list = userStatisticDetailService.getUserCollectionFund(userId, maxCreateTime, limit);
		} catch (Exception e) {
			logger.error("userCollectionService.getUserCollectionFund", e);
		}
		return list;
	}
	
	
	@Override
	public List<IntentionStatistic> getUserCollectionIntention(Long userId,Date maxCreateTime,Integer limit){
		List<IntentionStatistic> list = new ArrayList<IntentionStatistic>();
		try {
			list = userStatisticDetailService.getUserCollectionIntention(userId, maxCreateTime, limit);
		} catch (Exception e) {
			logger.error("userCollectionService.getUserCollectionFund", e);
		}
		return list;
	}

	@Override
	public CollectionResult doCollectionAdd(UserStatisticDetail userStatisticDetail) {
		CollectionResult result = new CollectionResult();
		boolean bool;
		try {
			bool = this.userStatisticDetailService
					.doCollection(userStatisticDetail);
			if(!bool){
				result.setMsg("关注失败");
				return result;
			}
			result.setSuccess(true);
			return result;

		} catch (Exception e) {
			logger.error("userCollectionService.doCollectionInformation", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

	@Override
	public CollectionResult doCollectionCancel(UserStatisticDetail userStatisticDetail) {
		CollectionResult result = new CollectionResult();
		try {
			boolean bool = this.userStatisticDetailService
					.doCancelCollection(userStatisticDetail);
			if (!bool) {
				result.setMsg("取消关注失败");
				return result;
			}
			result.setSuccess(true);
			return result;
		} catch (Exception e) {
			logger.error("userCollectionService.doCancelCollection", e);
			result.setMsg(e.getMessage());
			return result;		}	
		
	}

	@Override
	public List<MyList> getMyProjects(Long userId,Date pushTime,Integer limit) {
		List<MyList> myList = new ArrayList();
		List<Project> list = projectService.queryPageByOwnerId(userId.toString(), pushTime, limit, StatusEnums.RELEASE.getCode());
		if(CollectionUtils.isNotEmpty(list)){
			for(Project project:list){
				MyList my = new MyList();
				my.setId(project.getId());
				my.setPic(project.getPic());
				my.setPushTime(project.getPushtime());
				my.setTitle(project.getProjectName());
				CommonCounterKey commonCounterKey = new CommonCounterKey();
				commonCounterKey.setMainKey(UserStatisticDetail.Type.pro_col.getCountKey());
				commonCounterKey.setSecondaryKey(project.getId().toString());
				my.setCollectNum(commonCounterService.getCurrentCountByKey(commonCounterKey));
				commonCounterKey.setMainKey(UserStatisticDetail.Type.pro_advisory.getCountKey());
				my.setQueryNum(commonCounterService.getCurrentCountByKey(commonCounterKey));
				myList.add(my);
			}
		}
		return myList;
	}

	@Override
	public List<MyList> getMyFunds(Long userId,Date pushTime,Integer limit) {
		List<MyList> myList = new ArrayList();
		FundDto fundDto = new FundDto();
		fundDto.setEndTime(pushTime);
		fundDto.setUserId(userId);
		fundDto.setState(StatusEnums.RELEASE.getCode());
		fundDto.setPageNum(1);
		fundDto.setPageSize(limit);
		List<Fund> fundList = fundService.queryByCondition(fundDto);
		if(CollectionUtils.isNotEmpty(fundList)){
			for(Fund fund:fundList){
				MyList my = new MyList();
				my.setId(fund.getId());
				my.setPushTime(fund.getPushTime());
				my.setTitle(fund.getTitlesVal());
				CommonCounterKey commonCounterKey = new CommonCounterKey();
				commonCounterKey.setMainKey(UserStatisticDetail.Type.fund_col.getCountKey());
				commonCounterKey.setSecondaryKey(fund.getId().toString());
				my.setCollectNum(commonCounterService.getCurrentCountByKey(commonCounterKey));
				commonCounterKey.setMainKey(UserStatisticDetail.Type.fund_advisory.getCountKey());
				my.setQueryNum(commonCounterService.getCurrentCountByKey(commonCounterKey));
				myList.add(my);
			}
		}
		return myList;
	}
	

	@Override
	public Map<String,Object> getStatisticMap(Long uid){
		Map<String,Object> map = new HashMap<String, Object>();
		Integer collectProject = userStatisticDetailService.getCountByType(UserStatisticDetail.Type.pro_col.getCode(),uid);
		Integer collectFund = userStatisticDetailService.getCountByType(UserStatisticDetail.Type.fund_col.getCode(),uid);
		Long collectedNumber = 0L;
		List<Intention> intentionlist = intentionService.selectByUserId(uid);
		Map<Long,Long> counterMap = commonCounterService.getListByMainKey(CommonCounterKey.MainKeyType.INTENTION_COL_COUNT.name());
		if(CollectionUtils.isNotEmpty(intentionlist)){
			for(Intention intention:intentionlist){
				if(counterMap.containsKey(intention.getId())){
					collectedNumber=collectedNumber+counterMap.get(intention.getId());
				}
			}
		}
		map.put("collectedNumber", collectedNumber);
		map.put("collectProject",collectProject);
		map.put("collectFund", collectFund);

		return map;
	}

}
